<?php
namespace app\api\controller;

use addons\wanlshop\library\WanlPay\WanlPay;
use app\admin\model\user\Evaluate;
use app\admin\model\user\worker\Type;
use app\admin\model\user\worker\Order;
use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Vip;
use fast\Date;

use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 工人
 */
class Worker extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
   
	/**
	 * 获取技能类型
	 * 
	 */
	public function get_type_list()
	{
	    $list = Type::where('status', 0)
	        ->field('id,name')
	        ->select();
		$this->success('OK', $list);
	}

    /**
     * 获取技能详情
     *
     
     */
    public function get_self_info()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $user_id = $this->auth->id;
//        $user_id=10;
        $info = \app\admin\model\user\Worker::where('user_id', $user_id)
            ->field('id,name,avatar,year,type_id,lat,lng,user_id,images,area_id,address,desc,mobile')
            ->find();
        $user_re=(new \app\admin\model\User())->where('id',$user_id)->find();
        $is_vip =$user_re['is_vip'];
        if($info){
            //用户详情
            $user_ping_count_re=(new Evaluate())->where('worker_user_id',$info['user_id'])->select();
            $user_ping_data=[];
            if($user_ping_count_re){
                $user_ids=array_column($user_ping_count_re,'user_id');
                $user_list_re=(new \app\admin\model\User())->whereIn('id',$user_ids)->select();
                $user_list=[];
                foreach($user_list_re as $v){
                    $user_list[$v['id']]=[
                        'user_id'=>$v['id'],
                        'nickname'=>$v['nickname'],
                        'avatar'=>$v['avatar'],
                    ];
                }
                foreach ($user_ping_count_re as $v){
                    $user_data=$user_list[$v['user_id']];
                    $this_data=[
                        'user_id'=>$v['user_id'],
                        'star'=>$v['star'],
                        'images'=>$v['images'],
                        'content'=>$v['content'],
                        'nickname'=>$user_data['nickname'],
                        'avatar'=>$user_data['avatar'],
                    ];
                    $user_ping_data[]=$this_data;
                }
            }

            $type_re=(new Type())->whereIn('id',$info['type_id'])->find();
            $type_name =$type_re['name'];
            $info['type_name']=$type_name;
            $user_ping_count =count($user_ping_data);
            $info['is_vip']=$is_vip;
            $info['user_ping_count']=$user_ping_count;
            $info['user_ping_data']=$user_ping_data;
            $info['images_data']=explode(',',$info['images']);
            //
            $area_info=Area::where(['id'=>$info['area_id']])->find();
            $area=explode(',',$area_info['mergename']);
            $info['area_info']=$area[1].$area[2].$area[3];
        }else{
            $info=[
                'status'=>-1,
                'is_vip'=>$is_vip,
                'name'=>'',
                'type_id'=>'',
                'type_name'=>'',
                'user_ping_count'=>0,
                'user_ping_data'=>[],
                'images_data'=>[],
                'desc'=>'',
                'images'=>'',
                'mobile'=>'',
                'year'=>'',
                'address'=>'',
                'avatar'=>'',
                'lat'=>'',
                'lng'=>'',
            ];
        }

        $this->success('OK', $info);
    }
	
	
	/**
	 * 提交技能
	 * 
	 */
	public function add()
	{
	    if ($this->request->isPost()) {
$user_id = $this->auth->id;
//            $user_id=4;
            $data = $this->request->post();
            $validate=new \app\api\validate\Worker();
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            if(!empty($data['images'])){
                $images=explode(',',$data['images']);
                foreach ($images as $v){
                    $data1=['images'=>$v];
                    if (!$validate->scene('add_images')->check($data1)) {
                        $this->error($validate->getError());
                    }
                }
            }
            $area_info=Area::where(['id'=>$data['area_id']])->find();
            if(!$area_info){
                $this->error('获取省市区信息失败');
            }
            if($area_info['level']!=3){
                $this->error('省市区id格式错误');
            }

            $insert_data=[
                'user_id'=>$user_id,
                'name'=>$data['name'],
                'type_id'=>$data['type_id'],
                'area_id'=>$data['area_id'],
                'desc'=>$data['desc'],
                'images'=>$data['images'],
                'mobile'=>$data['mobile'],
                'year'=>$data['year'],
                'address'=>$data['address'],
                'avatar'=>$data['avatar'],
                'lat'=>$data['lat'],
                'lng'=>$data['lng'],
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
            ];
	        $lastdata = \app\admin\model\user\Worker::where('user_id', $this->auth->id)->find();
	        $res=false;
	        $id=0;
            Db::startTrans();
            try {
                if($lastdata){
                    $id=(new \app\admin\model\user\Worker())::where(['user_id'=>$user_id])->update($insert_data);
                }else{
                    $id=(new \app\admin\model\user\Worker())::insertGetId($insert_data);
                }
                if($id){
                    $res=true;
                    Db::commit();
                }else{
                    Db::rollback();
                }
            } catch (Exception $e) {
                Db::rollback();
            }
            if(!$res){
                $this->error('提交失败,请稍后重试');
            }
            $this->success('提交成功',['id'=>$id]);
	    }
	    $this->error("请求错误");
	}

    /**
     * 获取工人列表
     *
     
     */
    public function get_list()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $page=isset($data['page']) &&!empty($data['page'])?(int)$data['page']:1;
        $limit=isset($data['limit']) &&!empty($data['limit'])?(int)$data['limit']:10;
        $lat=isset($data['lat']) &&!empty($data['lat'])?$data['lat']:'';
        $lng=isset($data['lng']) &&!empty($data['lng'])?$data['lng']:'';
        $area_id=isset($data['area_id']) &&!empty($data['area_id'])?$data['area_id']:0;
        $type_id=isset($data['type_id']) &&!empty($data['type_id'])?$data['type_id']:0;
        $name=isset($data['name']) &&!empty($data['name'])?$data['name']:0;
        $where="w.status = 1";
        if($area_id){
            $where .=' and w.area_id='.$area_id;
        }
        if($type_id){
            $where .=' and w.type_id='.$type_id;
        }
        if($name){
            $where .=" and w.name like '%$name%'";
        }
        $list = \app\admin\model\user\Worker::alias('w')
            ->join('user u','u.id=w.user_id')
            ->where($where)
            ->field('w.id,w.name,w.avatar,w.year,w.type_id,w.lat,w.lng,w.user_id,u.is_vip')
            ->order('u.is_vip desc,w.id desc')
            ->page($page,$limit)
            ->select();
        $type_ids=array_column($list,'type_id');
        $user_ids=array_column($list,'user_id');
        //用户详情
        $user_data=[];
        if(!empty($user_ids)){
            $user_re=(new \app\admin\model\User())->whereIn('id',$user_ids)->select();
            //评论数量
            $user_ping_count_re=(new Evaluate())->field('count(*) as user_count,worker_user_id')->whereIn('worker_user_id',$user_ids)->group('worker_user_id')->select();
            //好评数量
            $user_ping_count_re1=(new Evaluate())
                ->field('count(*) as user_count,worker_user_id')
                ->whereIn('worker_user_id',$user_ids)
                ->where('star','=',5)
                ->group('worker_user_id')->select();
            $user_ping_count_data=[];
            foreach ($user_ping_count_re as $v){
                $user_ping_count_data[$v['worker_user_id']]=[
                    'user_id'=>$v['worker_user_id'],
                    'user_count'=>$v['user_count'],
                ];
            }
            $user_hao_ping_count_data=[];
            foreach ($user_ping_count_re1 as $v){
                $user_hao_ping_count_data[$v['worker_user_id']]=[
                    'user_id'=>$v['worker_user_id'],
                    'user_count'=>$v['user_count'],
                ];
            }

            foreach ($user_re as $v){
                $user_data[$v['id']]=[
                    'id'=>$v['id'],
                    'username'=>$v['username'],
                    'is_vip'=>$v['is_vip'],
                    'user_ping_count'=>isset($user_ping_count_data[$v['id']])?$user_ping_count_data[$v['id']]['user_count']:0,
                    'user_hao_ping_count'=>isset($user_hao_ping_count_data[$v['id']])?$user_hao_ping_count_data[$v['id']]['user_count']:0,
                ];
            }
        }

        $type_data=[];
        if(!empty($type_ids)){
            $type_re=(new Type())->whereIn('id',$type_ids)->select();
            foreach ($type_re as $v){
                $type_data[$v['id']]=[
                    'id'=>$v['id'],
                    'name'=>$v['name'],
                ];
            }
        }
        foreach ($list as $k=>$v){
            $type_name='';
            if(isset($type_data[$v['type_id']])){
                $type_name =$type_data[$v['type_id']]['name'];
            }
            $v['type_name']=$type_name;
            $is_vip =$user_data[$v['user_id']]['is_vip'];
            $user_ping_count =$user_data[$v['user_id']]['user_ping_count'];
            $user_hao_ping_count =$user_data[$v['user_id']]['user_hao_ping_count'];
            $haoping=100;
            if($user_ping_count>0){
                if($user_hao_ping_count==0){
                    $haoping=0;
                }else{
                    $haoping=bcdiv($user_hao_ping_count,$user_ping_count,4);
                    $haoping=bcmul($haoping,100,4);
                }
            }
            $v['is_vip']=$is_vip;
            $v['user_ping_count']=$user_ping_count;
            $v['user_hao_ping_count']=$haoping;
            $distance=0;
            if(!empty($lng) && !empty($lat)){
                $distance = bcmul(getDistance(floatval($lat),floatval($lng),floatval($v['lat']),floatval($v['lng'])),1000,0);  //计算两点间距离 米
                $distance=bcdiv($distance,1000,2);
            }
            $v['distance']=$distance;
            $list[$k]=$v;
        }
        $this->success('OK', $list);
    }

    /**
     * 获取工人详情
     *
     
     */
    public function get_info()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $id=isset($data['id']) &&!empty($data['id'])?$data['id']:0;
        $lat=isset($data['lat']) &&!empty($data['lat'])?$data['lat']:'';
        $lng=isset($data['lng']) &&!empty($data['lng'])?$data['lng']:'';
        $info = \app\admin\model\user\Worker::where('id', $id)
            ->field('id,name,avatar,year,type_id,lat,lng,user_id,images,address,area_id,desc')
            ->find();
        if(!$info){
            $this->error('获取失败');
        }
        $area_info=Area::where(['id'=>$info['area_id']])->find();
        $area_arr=explode(',',$area_info['mergename']);
        $info['address_area']=$area_arr[1].$area_arr[2].$area_arr[3];
        //用户详情
        $user_re=(new \app\admin\model\User())->where('id',$info['user_id'])->find();
        $user_ping_count_re=(new Evaluate())->where('worker_user_id',$info['user_id'])->select();
        $user_ping_data=[];
        $fen=5;
        $man_count=0;
        if($user_ping_count_re){
            $user_ids=array_column($user_ping_count_re,'user_id');
            $user_list_re=(new \app\admin\model\User())->whereIn('id',$user_ids)->select();
            $user_list=[];
            foreach($user_list_re as $v){
                $user_list[$v['id']]=[
                    'user_id'=>$v['id'],
                    'nickname'=>$v['nickname'],
                    'avatar'=>$v['avatar'],
                ];
            }
            foreach ($user_ping_count_re as $v){
                $user_data=$user_list[$v['user_id']];
                if($v['star']==5){
                    $man_count+=1;
                }
                $this_data=[
                    'user_id'=>$v['user_id'],
                    'star'=>$v['star'],
                    'images'=>$v['images'],
                    'content'=>$v['content'],
                    'nickname'=>$user_data['nickname'],
                    'avatar'=>$user_data['avatar'],
                    'create_time_text'=>date('Y-m-d H:i:s',$v['create_time']),
                ];
                $user_ping_data[]=$this_data;
            }
        }
        $total_count=count($user_ping_data);
        if($total_count>0){
            $fen=bcdiv($man_count,$total_count,4);
            $fen=bcmul($fen,5,2);
        }

        $type_re=(new Type())->whereIn('id',$info['type_id'])->find();
        $type_name =$type_re['name'];
        $info['type_name']=$type_name;
        $info['fen']=$fen;
        $is_vip =$user_re['is_vip'];
        $user_ping_count =count($user_ping_data);
        $info['is_vip']=$is_vip;
        $info['user_ping_count']=$user_ping_count;
        $info['user_ping_data']=$user_ping_data;
        $info['images_data']=explode(',',$info['images']);
        $distance=0;
        if(!empty($lng) && !empty($lat)){
            $distance = bcmul(getDistance($lat,$lng,$info['lat'],$info['lng']),1000,0);  //计算两点间距离
            $distance=bcdiv($distance,1000,2);
        }
        $info['distance']=$distance;
        $this->success('OK', $info);
    }

    /**
     * 预约工匠
     *
     */
    public function add_order()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        if ($this->request->isPost()) {
            $user_id = $this->auth->id;
//            $user_id=4;
            $data = $this->request->post();
            $validate=new \app\api\validate\Worker();
            if (!$validate->scene('add_order')->check($data)) {
                $this->error($validate->getError());
            }
            $day_time=strtotime($data['day'].' '.$data['time'].':00');
            if(!$day_time){
                $this->error('预约日期格式有误');
            }
            //检测工人信息
            $worker_info=\app\admin\model\user\Worker::where(['user_id'=>$data['worker_user_id']])->find();
            if(!$worker_info){
                $this->error('获取工人信息失败');
            }
            if($data['worker_user_id']==$user_id){
                $this->error('不可预约');
            }
            $insert_data=[
                'user_id'=>$user_id,
                'worker_user_id'=>$data['worker_user_id'],
                'day'=>$data['day'],
                'day_time'=>$day_time,
                'time'=>$data['time'],
                'server_content'=>$data['server_content'],
                'mobile'=>$data['mobile'],
                'address'=>$data['address'],
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>0,
            ];
            $lastdata = Order::where(['user_id'=> $this->auth->id,'worker_user_id'=>$insert_data['worker_user_id'],'status'=>0])->find();
            if($lastdata){
                $this->error('已提交过!');
            }
            $res=false;
            Db::startTrans();
            try {
                $re=(new Order())::insert($insert_data);
                if($re){
                    $res=true;
                    Db::commit();
                }else{
                    Db::rollback();
                }
            } catch (Exception $e) {
                Db::rollback();
            }
            if(!$res){
                $this->error('提交失败,请稍后重试');
            }
            $this->success('提交成功',[]);
        }
        $this->error("请求错误");
    }

    /**
     * 预约列表
     *

     */
    public function get_yu_list()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $page=isset($data['page']) &&!empty($data['page'])?(int)$data['page']:1;
        $limit=isset($data['limit']) &&!empty($data['limit'])?(int)$data['limit']:10;
        $start_time=isset($data['start_time']) &&!empty($data['start_time'])?$data['start_time']:'';
        $end_time=isset($data['end_time']) &&!empty($data['end_time'])?$data['end_time']:'';
        $where="o.user_id = ".$user_id;
        $where .=' and o.status <> -1';
        if(!empty($start_time) && !empty($end_time)){
            $start_time_str=strtotime($start_time);
            $end_time_str=strtotime($end_time);
            $where .=' and o.day_time >'.$start_time_str;
            $where .=' and o.day_time <'.$end_time_str;

        }

        //处理日期时间
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.worker_user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where($where)
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.address,u.nickname,uw.name,uw.avatar,t.name as worker_type_name')
            ->page($page,$limit)
            ->select();
        foreach ($list as $k=>$v){
            $v['order_status']=Order::getWebStatusList(1,$v['status']);
            $list[$k]=$v;
        }
        $this->success('OK', $list);
    }

    /**
     * 预约详情
     *

     */
    public function get_yu_info()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $id=isset($data['id']) &&!empty($data['id'])?(int)$data['id']:0;
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where(['o.id'=> $id,'o.user_id'=>$user_id])
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,uw.year,t.name as worker_type_name')
            ->find();

        if(!$list){
            $this->error('获取失败');
        }
        $list['mobile']=empty($list['mobile'])?'无':hidestr($list['mobile'],3,4);
        $list['order_status']=Order::getWebStatusList(1,$list['status']);

        $this->success('OK', $list);
    }

    /**
     * 取消预约
     *

     */
    public function cancel_yu()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $id=isset($data['id']) &&!empty($data['id'])?(int)$data['id']:1;
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.worker_user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where(['o.id'=> $id,'o.user_id'=>$user_id])
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,t.name as worker_type_name')
            ->find();
        if(!$list){
            $this->error('获取失败');
        }
        if($list['status']!=0){
            $this->error('该订单状态不可操作');
        }
        $re=Order::where(['id'=>$list['id']])->update(['status'=>-1,'update_time'=>time()]);
        if(!$re){
            $this->error('取消失败');
        }
        $this->success('取消成功');
    }

    /**
     * 邀约列表
     *

     */
    public function get_yao_list()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $page=isset($data['page']) &&!empty($data['page'])?(int)$data['page']:1;
        $limit=isset($data['limit']) &&!empty($data['limit'])?(int)$data['limit']:10;
        $start_time=isset($data['start_time']) &&!empty($data['start_time'])?$data['start_time']:'';
        $end_time=isset($data['end_time']) &&!empty($data['end_time'])?$data['end_time']:'';
        $where="o.user_id = ".$user_id;
        $where .=' and o.status <> -2';
        if(!empty($start_time) && !empty($end_time)){
            $start_time_str=strtotime($start_time);
            $end_time_str=strtotime($end_time);
            $where .=' and o.day_time >'.$start_time_str;
            $where .=' and o.day_time <'.$end_time_str;

        }
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.user_id')
            ->join('fa_user u','u.id=o.user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where($where)
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,t.name as worker_type_name')
            ->page($page,$limit)
            ->select();
        foreach ($list as $k=>$v){
            $v['order_status']=Order::getWeb1StatusList(1,$v['status']);
            $list[$k]=$v;
        }
        $this->success('OK', $list);
    }

    /**
     * 邀约详情
     *

     */
    public function get_yao_info()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $id=isset($data['id']) &&!empty($data['id'])?(int)$data['id']:1;
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where(['o.id'=> $id,'o.worker_user_id'=>$user_id])
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,uw.year,t.name as worker_type_name')
            ->find();
        if(!$list){
            $this->error('获取失败');
        }
        $list['mobile']=empty($list['mobile'])?'无':hidestr($list['mobile'],3,4);
        $list['order_status']=Order::getWeb1StatusList(1,$list['status']);

        $this->success('OK', $list);
    }
    /**
     * 取消邀约
     *

     */
    public function cancel_yao()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $id=isset($data['id']) &&!empty($data['id'])?(int)$data['id']:1;
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.worker_user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where(['o.id'=> $id,'o.worker_user_id'=>$user_id])
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,t.name as worker_type_name')
            ->find();
        if(!$list){
            $this->error('获取失败');
        }
        if($list['status']!=0){
            $this->error('该订单状态不可操作');
        }
        $re=Order::where(['id'=>$list['id']])->update(['status'=>-1,'update_time'=>time()]);
        if(!$re){
            $this->error('取消失败');
        }
        $this->success('取消成功');
    }
    /**
     * 完成订单
     *

     */
    public function success_yao()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $data=$this->request->post();
        $user_id = $this->auth->id;
//        $user_id=4;
        $id=isset($data['id']) &&!empty($data['id'])?(int)$data['id']:1;
        $list = Order::alias('o')
            ->join('fa_user_worker uw','uw.user_id=o.worker_user_id')
            ->join('fa_user u','u.id=o.worker_user_id')
            ->join('fa_user_worker_type t','t.id=uw.type_id')
            ->where(['o.id'=> $id,'o.worker_user_id'=>$user_id])
            ->field('o.id,o.status,o.worker_user_id,o.day,o.time,o.server_content,o.mobile,o.address,u.nickname,uw.name,uw.avatar,t.name as worker_type_name')
            ->find();
        if(!$list){
            $this->error('获取失败');
        }
        if($list['status']!=0){
            $this->error('该订单状态不可操作');
        }
        $re=Order::where(['id'=>$list['id']])->update(['status'=>3,'update_time'=>time()]);
        if(!$re){
            $this->error('失败');
        }
        $this->success('成功');
    }

    /**
     * 提交评价
     *
     */
    public function add_ping()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        if ($this->request->isPost()) {
            $user_id = $this->auth->id;
//            $user_id=4;
            $data = $this->request->post();
            $validate=new \app\api\validate\Worker();
            if (!$validate->scene('add_ping')->check($data)) {
                $this->error($validate->getError());
            }
            if(!empty($data['images'])){
                $images=explode(',',$data['images']);
                foreach ($images as $v){
                    $data1=['images'=>$v];
                    if (!$validate->scene('add_images')->check($data1)) {
                        $this->error($validate->getError());
                    }
                }
            }
            $order_info=Order::where(['id'=>$data['order_id'],'user_id'=>$user_id])->find();
            if(!$order_info){
                $this->error('获取订单失败');
            }

            $insert_data=[
                'user_id'=>$order_info['user_id'],
                'worker_user_id'=>$order_info['worker_user_id'],
                'content'=>$data['content'],
                'star'=>$data['star'],
                'images'=>$data['images'],
                'order_id'=>$data['order_id'],
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>0,
            ];
            $lastdata = Evaluate::where('order_id', $data['order_id'])->find();
            if($lastdata){
                $this->error('已提交过!');
            }
            $res=false;
            Db::startTrans();
            try {
                $re=(new Evaluate())::insert($insert_data);
                if($re){
                    $res=true;
                    Db::commit();
                }else{
                    Db::rollback();
                }
            } catch (Exception $e) {
                Db::rollback();
            }
            if(!$res){
                $this->error('提交失败,请稍后重试');
            }
            $this->success('提交成功',[]);
        }
        $this->error("请求错误");
    }
    
    /**
     * 会员金额
     *
     */
    public function pay_money()
    {
        $money=config('site.vip_money');
        $this->success('请求成功',['money'=>$money]);
    }
    
    /**
     * 会员
     *
     */
    public function pay()
    {
        if(!$this->request->isPost()){
            $this->error('请求失败');
        }
        $type = $this->request->post('type');
        $method = $this->request->post('method');
        $code = $this->request->post('code');
        $user_id = $this->auth->id;
//        $user_id = 4;
        $type ? $type : ($this->error(__('未选择支付类型')));
        $money=config('site.vip_money');
//
//        $res=false;
//        Db::startTrans();
//        try {
//            $re=(new Vip())::insert($insert_data);
//            $re1=(new \app\admin\model\User())::where(['id'=>$user_id])->update(['is_vip'=>1,'updatetime'=>time()]);
//            if($re &&$re1){
//                $res=true;
//                Db::commit();
//            }else{
//                Db::rollback();
//            }
//        } catch (Exception $e) {
//            Db::rollback();
//        }
//        if(!$res){
//            $this->error('提交失败,请稍后重试');
//        }

        // 调用支付
        $wanlPay = new WanlPay($type, $method, $code);
        $data = $wanlPay->vip($user_id,$money);
        if($data['code'] == 200){
            $this->success($data['msg'], $data['data']);
        }else{
            $this->error($data['msg']);
        }
        $this->error("请求错误");
    }

}
